package com.tool;

/**
 * @author: tzj
 * @create: 2022-10-19
 * CRC16算法
 **/
public class CRC16Util {

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            final int value = crc16(String.valueOf(i));
            System.out.println(value % 16384);
        }
    }


    public static int crc16(String str) {
        final byte[] bytes = str.getBytes();
        int crc = 0xFFFF;
        for (int j = 0; j < bytes.length; j++) {
            crc = ((crc >>> 8) | (crc << 8)) & 0xffff;
            crc ^= (bytes[j] & 0xff);
            crc ^= ((crc & 0xff) >> 4);
            crc ^= (crc << 12) & 0xffff;
            crc ^= ((crc & 0xFF) << 5) & 0xffff;
        }
        crc &= 0xffff;
        return crc;

    }
}
